﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using SharePoint2007Migration.Costants;
using SharePoint2007Migration.Domain.Collections;

namespace SharePoint2007Migration
{
    public static class IssuesManager
    {


        public static IssueCollection LoadIssues()
        {
            return LoadIssues(Path.Combine(ConfigHelper.SourceReportFilePath, ConfigHelper.SourceReportFileName));
        }

        public static IssueCollection LoadIssues(string ReportPath)
        {
           
            TextReader tr = new StreamReader(ReportPath);
            String text = tr.ReadToEnd();
            tr.Close();
            return ReportToIssues(text);
            #region commens


            ////try
            ////{
            ////    LogHelper.TraceDebug("IN");
            ////    TextReader tr = new StreamReader(ReportPath);
            ////    String text = tr.ReadToEnd();
            ////    tr.Close();
            ////}
            ////catch (Exception exc)
            ////{
            ////    LogHelper.TraceError(exc);
            ////    throw;
            ////}
            ////finally
            ////{
            ////    LogHelper.TraceDebug("OUT");
            ////}
            //return IssuesToReturn; 
            #endregion
        }

        private static IssueCollection ReportToIssues(string reportText)
        {
            IssueCollection IssuesToReturn = new IssueCollection();
             string[] report = reportText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

             for (int i = 0; i < report.Length; i++)
             {
                 Match match = Regex.Match(report[i], RegexCostants.REGEX_CATEGORY_MATCH);
                 if (match.Success)
                 {
                    StringBuilder IssueText = new StringBuilder();
                    for (int k = i; k < report.Length; k++)
                    {
                        if (report[k] == String.Empty)
                        {
                            IssuesToReturn.Add(new BaseIssue(IssueText.ToString()));
                            i = k;
                            break;
                        }
                        else IssueText.AppendLine(report[k]);
                    }
                 }
             }
             return IssuesToReturn;
        }

    }
}
